package com.wondersgroup.fjzlpt.tbxt.controller.rest.yltfwtz;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.wondersgroup.fjzlpt.tbxt.beans.req.yltfwtz.QueryPageReqBean;
import com.wondersgroup.fjzlpt.tbxt.beans.req.yltfwtz.SaveReqBean;
import com.wondersgroup.fjzlpt.tbxt.beans.resp.BaseRespBean;
import com.wondersgroup.fjzlpt.tbxt.beans.resp.yltfwtz.QueryPageRespBean;
import com.wondersgroup.fjzlpt.tbxt.service.inter.yltfwtz.YltfwtzService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 医联体服务台账restController
 * <p>
 * <p>
 * Created by sanma on 2017/9/18.
 */
@RestController
@RequestMapping("/ylttz")
@Api(value = "医联体服务台账标准接口", tags = "YltfwztRestController", description = "医联体服务台账API")
public class YltfwztRestController {

    @Autowired
    private YltfwtzService yltfwtzService;

    /**
     * 保存或修改
     *
     * @param request
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "保存类型(add/update)",paramType = "query")
    })
    @ApiOperation(value = "保存或修改", notes = "保存或修改医联体服务台账", response = BaseRespBean.class)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONObject save(HttpServletRequest request, ArrayList<SaveReqBean> rows) {
        QuickPrincipal principal = (QuickPrincipal) request.getUserPrincipal();
        JSONObject userInfo = principal.getUserInfo();
        Map map = RequestUtils.GetParameterMap(request);
        //由于字段名称不对应，所以一个一个加
        map.put("PTJGID", userInfo.getString("PTJGID"));
        map.put("JGMC", userInfo.getString("YLJGMC"));
        map.put("JGLB", userInfo.getString("WSJGLBDM"));
        map.put("XZQHDM", userInfo.getString("JGXZQHDM"));
        //map.put("FWRYGH",userInfo.getString("YSGH"));
        map.put("USERID", userInfo.getString("USERID"));
        map.put("USERNAME", userInfo.getString("XM"));
        map.put("ZT", "1");
        return yltfwtzService.save(map);
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询医联体服务台账", response = QueryPageRespBean.class)
    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    public JSONObject queryPage(HttpServletRequest request, QueryPageReqBean queryPageReqBean) {
        QuickPrincipal principal = (QuickPrincipal) request.getUserPrincipal();
        JSONObject userInfo = principal.getUserInfo();

        Map map = RequestUtils.GetParameterMap(request);
        //由于字段名称不对应，所以一个一个加
        map.put("PTJGID", userInfo.getString("PTJGID"));
        map.put("JGLB", userInfo.getString("WSJGLBDM"));
        map.put("XZQHDM", userInfo.getString("JGXZQHDM"));
        return yltfwtzService.queryPage(map);
    }

    /**
     * 根据主键ID删除数据（不可恢复）
     *
     * @param request
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ID", value = "台账ID" , required = true, paramType = "query")
    })
    @ApiOperation(value = "删除", notes = "删除医联体服务台账数据（不可恢复）",response = BaseRespBean.class)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JSONObject delete(HttpServletRequest request) {
        Map map = RequestUtils.GetParameterMap(request);
        return yltfwtzService.delete(map);
    }

    /**
     * 下载Excel模板
     *
     * @param response
     * @return
     */
    @ApiOperation(value = "下载模板", notes = "下载Excel模板，用于上传数据时使用")
    @RequestMapping(value = "downExcelModel", method = RequestMethod.POST)
    public void downExcelModel(HttpServletRequest request, HttpServletResponse response) {
        yltfwtzService.downExcelModel(request, response);
    }

    /**
     * 解析上传的Excel数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "解析Excel", notes = "上传Excel文件并解析返回文件数据")
    @RequestMapping(value = "parseExcalData", method = RequestMethod.POST)
    public String parseExcalData(@RequestParam(value = "fileField", required = true) MultipartFile file, HttpServletRequest request) {
        String str = yltfwtzService.parseExcalData(file, request);
        return str;
    }
}
